package com.lagou.dao;

import com.lagou.entity.Account;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class AccountDao {

    // 需求： 判断转出方是否有足够的余额并转账

    public Account findAccount(String aid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        // 编写SQL 判断账户余额
        String sql = "select * from account where aid = ? and balance >= 5000";

        // 执行查询
        Account account = qr.query(sql,new BeanHandler<Account>(Account.class), aid);

        return account;


    }

    // 修改账户余额
     public Account updateAccount(String s, String aid) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        // 编写SQL 更新转出账户
         String sql1 = "update account set balance = balance - 5000 where aid = 1122334455";

         // 执行更新
         qr.update(sql1);

         // 编写SQL 更新转入账户
         String sql2 = "update account set balance = balance + 5000 where aid = 5544332211";

         qr.update(sql2);

         return null;
     }

}
